package demo.trendline;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
import org.apache.commons.math3.util.FastMath;

/**
 * @author Arnold Yand
 * @since Mar 19, 2017 1:15:48 PM
 * @summary demo.trendline.TrendLineDemo.java
 */
public class TrendLineDemo {

  private Vector2D direction = new Vector2D(1, 0); // x-axis

  public TrendLineDemo() {

    double[][] node = { { 3, 11 }, { 4, 8 }, { 5, 5 }, { 6, 1 }, { 7, 2 }, { 8, 5 }, { 9, 6 }, { 10, 2 }, { 11, 0 }, { 12, 10 } };
    // // busy
    // node = new double[][] { { 3, 11 }, { 4, 8 }, { 5, 5 }, { 6, 1 } }; //
    System.out.println("--x , y--");
    for (double[] n : node) {
      System.out.println(n[0] + "," + n[1]);
    }

    double degrees = getTrend(node);
    String status = getTendStatus(degrees);
    System.out.println("angle with direction is " + degrees + ", and the staus is " + status);

  }

  private String getTendStatus(double degrees) {
    if (degrees <= 15) {
      return STATUS.IDLE.name();
    }
    if (degrees <= 40) {
      return STATUS.EASY.name();
    }
    if (degrees <= 55) {
      return STATUS.NORMAL.name();
    }
    return STATUS.BUSY.name();
  }

  private double getTrend(double[][] node) {
    List<Vector2D> vector = new ArrayList<Vector2D>();
    for (int i = 1; i < node.length; i++) {
      vector.add(new Vector2D(node[i][0] - node[i - 1][0], node[i][1] - node[i - 1][1]));
    }

    Vector2D tend = Vector2D.ZERO;
    for (Vector2D v : vector) {
      tend = tend.add(v);
    }

    System.out.println("tend vector " + tend);
    return FastMath.toDegrees(Vector2D.angle(this.direction, tend));
  }

  public static void main(String[] args) {
    new TrendLineDemo();
  }

  private enum STATUS {
    IDLE, EASY, NORMAL, BUSY
  }

}
